﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System.Collections.ObjectModel;
namespace AutoTestingFramework.Core.Controls
{
    public class RowWebElement : IWebElement
    {
        IWebElement m_Row;
        IReadOnlyCollection<IWebElement> m_Cells;
        //IReadOnlyCollection<RowWebElement> Cells;

        #region Base Methods, Not changed
        public void Clear()
        {
            m_Row.Clear();
        }

        public void Click()
        {
            m_Row.Click();
        }

        public bool Displayed
        {
            get { return m_Row.Displayed; }
        }

        public bool Enabled
        {
            get { return m_Row.Enabled; }
        }

        public string GetAttribute(string attributeName)
        {
            return m_Row.GetAttribute(attributeName);
        }

        public string GetCssValue(string propertyName)
        {
            return m_Row.GetAttribute(propertyName);
        }

        public System.Drawing.Point Location
        {
            get { return m_Row.Location; }
        }

        public bool Selected
        {
            get { return m_Row.Selected; }
        }

        public void SendKeys(string text)
        {
            m_Row.SendKeys(text);
        }

        public System.Drawing.Size Size
        {
            get { return m_Row.Size; }
        }

        public void Submit()
        {
            m_Row.Submit();
        }

        public string TagName
        {
            get { return m_Row.TagName; }
        }

        public string Text
        {
            get { return m_Row.Text; }
        }

        public IWebElement FindElement(By by)
        {
            return m_Row.FindElement(by);
        }

        public System.Collections.ObjectModel.ReadOnlyCollection<IWebElement> FindElements(By by)
        {
            return m_Row.FindElements(by);
        }
        #endregion

        internal RowWebElement(IWebElement Row)
        {
            if (Row.TagName.ToUpper() != "TR")
                throw new Exception("Invalid Element '" + Row.TagName + "'. Only tr element can be initialized");
            m_Row = Row;
        }
        public CellWebElement CellAt(int ColumnIndex)
        {
            IWebElement cell = FindElement(By.XPath(".//td|th[" + ColumnIndex.ToString() + "]"));
            return new CellWebElement(cell);
        }
        public IReadOnlyCollection<IWebElement> Cells
        {
            get
            {
                if (m_Cells == null)
                {
                    

                    try
                    {

                       m_Cells = FindElements(By.XPath("./td|th"));

                    }
                    catch (Exception ex)
                    {
                       
                            m_Cells = null;
                            return null;
                        
                    }


                  
                }
                var List = from Ce in m_Cells
                           select new CellWebElement(Ce);

                  return (new ReadOnlyCollection<CellWebElement>(List.ToList<CellWebElement>()));
                
            }
        }

       

    }
}
